<?php

namespace app\apiadmin\service\statics;

use MongoDB\BSON\UTCDateTime;
use think\facade\Db;

/**
 *
 * 记录直播数据 记录某一次直播中用户相关的数据
 *
 * 1 从直播开始计算,
 *      用户进入直播间
 *      用户离开直播间
 */
class LiveRecoderService
{
    /** @var MongoDbService */
    private $live_recoder_mongodb;

    public function __construct()
    {
        // $this->liveId = $liveId;
        $this->live_recoder_mongodb = MongoDBService::getInstance("live_recoder");
    }

    // 录制从command 接口中发送の的
    public function RecodeCommand($liveId, $commandStr, $param)
    {
        $recode = [
            "live_id" => $liveId,
            "type" => "command",
            "command" => $commandStr,
            "param" => json_encode($param),
            "create_time" => new UTCDateTime(microtime(true) * 1000),
        ];
        // 直接插入一条数据
        $this->live_recoder_mongodb->getCollection()->insertOne($recode);
    }

    public function RecodeChartMessage($groupId, $content)
    {
        $message = [
            // "live_id" => $this->liveId,
            "type" => "chart_message",
            "groupId" => $groupId,
            "content" => $content,
            "create_time" => new UTCDateTime(microtime(true) * 1000),
        ];

        $this->live_recoder_mongodb->getCollection()->insertOne($message);

    }
    //记录商品是否售罄消息
    public function RecodeCourseSellMessage($groupId, $content,$type='')
    {
        $message = [
            // "live_id" => $this->liveId,
            "type" => $type ?: "course_sell_message",
            "groupId" => $groupId,
            "content" => $content,
            "create_time" => new UTCDateTime(microtime(true) * 1000),
        ];

        $this->live_recoder_mongodb->getCollection()->insertOne($message);
        
    }
}